// mircoApp/serviceCenter/complaint/complaint.js
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
var app = getApp();

Page({
 
  /**
   * 页面的初始数据
   */
  behaviors: [skinBehavior],
  data: {
    id:'',
    tagList:[],
    relationForm:{
      imgs:[],
      mobile:'',
      email:'',
      content:'',
      problem_ids:[]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData();
    if(options.id) {
      this.setData({
        id:options.id
      })
      this.problemDetails();
    } else {
      this.problemData();
    }
    
  },
  //子组件事件
  tagClick(event) {
    let {index} = event.detail;
    this.data.tagList[index].checked = !this.data.tagList[index].checked;
    this.setData({
      tagList:this.data.tagList
    })
  },
  textChange(event) {
    let {val} = event.detail;
    this.setData({
      'relationForm.content':val
    })
  },
  onRead(event) {
    let {imgs} = event.detail;
    this.setData({
      'relationForm.imgs':imgs
    })
  },
  removeImg(event) {
    let {index} = event.detail;
    this.data.relationForm.imgs.splice(index,1);
    this.setData({
      'relationForm.imgs':this.data.relationForm.imgs
    });
  },
  initData() {
    this.setData({
      'relationForm.imgs':[],
      'relationForm.mobile':'',
      'relationForm.content':'',
      'relationForm.email':'',
      'relationForm.problem_ids':''
    });
  },
  inputChange(event) {
    let {name} = event.currentTarget.dataset;
    this.setData({
      [name]:event.detail
    })
  },
  problemData() {
    let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.proposal.index.problem-list");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          data.map((item,index) => {
            item.checked = false;
          })
          this.setData({
            tagList:data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  problemDetails() {
    let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.proposal.index.details");
    app._postNetWork({
      url: urlStr,
      data:{id:this.data.id},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          let problems = [];
          data.problems.map((item,index) => {
            problems.push({
             id:item.problem?.id,
             name:item.problem?.name,
             checked:true
            });
          })
          this.setData({
            'relationForm.imgs':data.imgs,
            'relationForm.content':data.content,
            'relationForm.email':data.email,
            'relationForm.mobile':data.mobile,
            tagList:problems
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  relationBtn() {
    let  problemArr = this.data.tagList.filter((item) => item.checked);
    this.data.relationForm.problem_ids = problemArr.map((item,index) => item.id);
    if (this.data.relationForm.problem_ids.length <= 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择问题类型',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.relationForm.content)) {
      wx.showToast({
        icon: 'none',
        title: '请输入问题信息',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.relationForm.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号',
        duration: 1500
      });
      return;
    }
    let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.proposal.index");
    app._postNetWork({
      url: urlStr,
      data:{form:this.data.relationForm},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          setTimeout(() => {
            this.onBack();
          },500);
          
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
   //返回上一页
   onBack() {
    const pages = getCurrentPages();
    if(pages.length === 1) {
      wx.navigateTo({ url:'/packageG/index/index'})										
    } else {
      wx.navigateBack({ delta: 1 })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  complainMe() {
    wx.navigateTo({
      url: '/mircoApp/serviceCenter/complaintList/complaintList',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})